import { NCascader } from "naive-ui";
import type { CascaderProps } from "naive-ui";
import type { BaseComponentType, OmitHTMLAttributes } from "../types";
import { getSlotsDom } from "../utils";

export type CascaderFactoryConstructorType = {
  props?: CascaderProps;
  attrs?: OmitHTMLAttributes<"onLoad">;
  actionSlot?: BaseComponentType;
  arrowSlot?: BaseComponentType;
  emptySlot?: BaseComponentType;
  notFoundSlot?: BaseComponentType;
};

export class CascaderFactory {
  private attrs: OmitHTMLAttributes<"onLoad"> = {};
  private props: CascaderProps = {};
  private actionSlot: BaseComponentType = null;
  private arrowSlot: BaseComponentType = null;
  private emptySlot: BaseComponentType = null;
  private notFoundSlot: BaseComponentType = null;

  constructor(data?: CascaderFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.actionSlot) this.setAction(data.actionSlot);
    if (data?.arrowSlot) this.setArrow(data.arrowSlot);
    if (data?.emptySlot) this.setEmpty(data.emptySlot);
    if (data?.notFoundSlot) this.setNotFound(data.notFoundSlot);
  }

  setAttrs(attrs: OmitHTMLAttributes<"onLoad">) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: CascaderProps) {
    this.props = props;
    return this;
  }

  setAction(component: BaseComponentType) {
    this.actionSlot = component;
    return this;
  }

  setArrow(component: BaseComponentType) {
    this.arrowSlot = component;
    return this;
  }

  setEmpty(component: BaseComponentType) {
    this.emptySlot = component;
    return this;
  }

  setNotFound(component: BaseComponentType) {
    this.notFoundSlot = component;
    return this;
  }

  create() {
    return (
      <NCascader {...this.props} {...this.attrs}>
        {{
          action: () => getSlotsDom(this.actionSlot),
          arrow: () => getSlotsDom(this.arrowSlot),
          empty: () => getSlotsDom(this.emptySlot),
          notFound: () => getSlotsDom(this.notFoundSlot),
        }}
      </NCascader>
    );
  }
}
